/////////////////////////////////////////////////////////////////////////////
// Name:        Mass_Diag.cpp
// Purpose:     Manager of the Aircraft Mass & Moment of Interial
// Author:      Matthew Gong
// Created:     05/22/2005
// Copyright:   (c) Matthew Gong
// Licence:     GPL licence
/////////////////////////////////////////////////////////////////////////////

#ifdef __GNUG__
// #pragma implementation
#endif

// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif

// -*- C++ -*- generated by wxGlade 0.3.5.1 on Sun May 22 14:59:18 2005

#include <wx/txtstrm.h>
#include <wx/datetime.h>

#include "Mass_Diag.h"

ClientData_pm::ClientData_pm()
  : weight(0), weight_unit(wxT("KG")),
    x(0),y(0),z(0),
    loc_unit(wxT("M"))
{
  name = wxString::Format(wxT("PointMass%lu"), wxDateTime::Now().GetTicks());
}

void ClientData_pm::Load(JSBSim::Element * el)
{
  name = el->GetAttributeValue("name");
  if (name.IsEmpty())
    name = wxT("Unknown");
  
  JSBSim::Element * temp_element;

  if (temp_element = el->FindElement("weight"))
  {
    weight = temp_element->GetDataAsNumber();
    weight_unit = temp_element->GetAttributeValue("unit");
    if (weight_unit.IsEmpty())
      weight_unit = wxT("LBS");
  }
  
  if (temp_element = el->FindElement("location"))
  {
    loc_unit = temp_element->GetAttributeValue("unit");
    if (loc_unit.IsEmpty())
      loc_unit = wxT("IN");
    x = temp_element->FindElementValueAsNumber("x");
    y = temp_element->FindElementValueAsNumber("y");
    z = temp_element->FindElementValueAsNumber("z");
  }
}

void ClientData_pm::Save(wxTextOutputStream & out, const wxString & prefix)
{
  out << prefix << wxT("<pointmass name=\"") << name << wxT("\">\n");
  
  wxString pre = prefix + wxT("    ");
  out << pre << wxT("<weight unit=\"") << weight_unit << wxT("\">") << wxString::Format(wxT("%g"), weight) << wxT("</weight>\n");
  out << pre << wxT("<location unit=\"") << loc_unit << wxT("\">\n");
  out << pre << wxT("    <x>") << wxString::Format(wxT("%g"), x) << wxT("</x>\n");
  out << pre << wxT("    <y>") << wxString::Format(wxT("%g"), y) << wxT("</y>\n");
  out << pre << wxT("    <z>") << wxString::Format(wxT("%g"), z) << wxT("</z>\n");
  out << pre << wxT("</location>\n");
  
  out << prefix << wxT("</pointmass>\n");
}

wxString ClientData_pm::GetText() const
{
  return wxString::Format("%s is %g %s at point [%g, %g, %g] in %s ", name.c_str(), weight, weight_unit.c_str(), x, y, z, loc_unit.c_str());
}

void ClientData_pm::ShowDialog()
{
  PMDialog dlg(NULL, -1 );
  dlg.Load(this);
  if (dlg.ShowModal() == wxID_OK)
  {
    dlg.Save(this);
  }
}


PMDialog::PMDialog(wxWindow* parent, int id, const wxString& title, const wxPoint& pos, const wxSize& size, long style):
    wxDialog(parent, id, title, pos, size, style)
{
    // begin wxGlade: PMDialog::PMDialog
    sizer_location_copy_staticbox = new wxStaticBox(this, -1, _("location"));
    label_name = new wxStaticText(this, -1, _("Name:"), wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT);
    text_ctrl_name = new wxTextCtrl(this, -1, wxT(""));
    label_weight = new wxStaticText(this, -1, _("Weight="), wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT);
    text_ctrl_weight = new wxTextCtrl(this, -1, wxT(""), wxDefaultPosition, wxDefaultSize, wxTE_RIGHT, wxTextValidator(wxFILTER_NUMERIC));
    const wxString combo_box_weight_choices[] = {
        wxT("KG"),
        wxT("LBS")
    };
    combo_box_weight = new wxComboBox(this, -1, wxT(""), wxDefaultPosition, wxDefaultSize, 2, combo_box_weight_choices, wxCB_DROPDOWN);
    label_location_x_copy = new wxStaticText(this, -1, _("x="));
    text_ctrl_location_x = new wxTextCtrl(this, -1, wxT(""), wxDefaultPosition, wxDefaultSize, 0, wxTextValidator(wxFILTER_NUMERIC));
    label_location_y_copy = new wxStaticText(this, -1, _("y="));
    text_ctrl_location_y = new wxTextCtrl(this, -1, wxT(""), wxDefaultPosition, wxDefaultSize, 0, wxTextValidator(wxFILTER_NUMERIC));
    label_location_z_copy = new wxStaticText(this, -1, _("z="));
    text_ctrl_location_z = new wxTextCtrl(this, -1, wxT(""), wxDefaultPosition, wxDefaultSize, 0, wxTextValidator(wxFILTER_NUMERIC));
    const wxString combo_box_location_choices[] = {
        wxT("M"),
        wxT("IN"),
        wxT("FT")
    };
    combo_box_location = new wxComboBox(this, -1, wxT(""), wxDefaultPosition, wxDefaultSize, 3, combo_box_location_choices, wxCB_DROPDOWN|wxCB_READONLY);
    button_ok = new wxButton(this, wxID_OK, _("OK"));
    button_cancel = new wxButton(this, wxID_CANCEL, _("Cancel"));

    set_properties();
    do_layout();
    // end wxGlade
}


void PMDialog::set_properties()
{
    // begin wxGlade: PMDialog::set_properties
    label_name->SetMinSize(wxSize(80, -1));
    text_ctrl_name->SetMinSize(wxSize(150, -1));
    label_weight->SetMinSize(wxSize(80, -1));
    text_ctrl_weight->SetMinSize(wxSize(80, -1));
    combo_box_weight->SetMinSize(wxSize(60, -1));
    combo_box_weight->SetSelection(0);
    text_ctrl_location_x->SetMinSize(wxSize(60, -1));
    text_ctrl_location_y->SetMinSize(wxSize(60, -1));
    text_ctrl_location_z->SetMinSize(wxSize(60, -1));
    combo_box_location->SetMinSize(wxSize(60, -1));
    combo_box_location->SetSelection(0);
    // end wxGlade
}


void PMDialog::do_layout()
{
    // begin wxGlade: PMDialog::do_layout
    wxBoxSizer* sizer_2 = new wxBoxSizer(wxVERTICAL);
    wxBoxSizer* sizer_7_copy = new wxBoxSizer(wxHORIZONTAL);
    wxStaticBoxSizer* sizer_location_copy = new wxStaticBoxSizer(sizer_location_copy_staticbox, wxHORIZONTAL);
    wxBoxSizer* sizer_3 = new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer* sizer_5 = new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer* sizer_4 = new wxBoxSizer(wxHORIZONTAL);
    sizer_4->Add(label_name, 0, wxALL|wxALIGN_CENTER_VERTICAL, 3);
    sizer_4->Add(text_ctrl_name, 0, wxALL|wxALIGN_CENTER_VERTICAL, 3);
    sizer_3->Add(sizer_4, 0, wxALL|wxEXPAND, 3);
    sizer_5->Add(label_weight, 0, wxALL|wxALIGN_CENTER_VERTICAL, 3);
    sizer_5->Add(text_ctrl_weight, 0, wxALL|wxALIGN_CENTER_VERTICAL, 3);
    sizer_5->Add(combo_box_weight, 0, wxALL|wxALIGN_CENTER_VERTICAL, 3);
    sizer_3->Add(sizer_5, 0, wxALL|wxEXPAND, 3);
    sizer_2->Add(sizer_3, 1, wxALL|wxEXPAND, 3);
    sizer_location_copy->Add(label_location_x_copy, 0, wxALL|wxALIGN_CENTER_VERTICAL, 3);
    sizer_location_copy->Add(text_ctrl_location_x, 1, wxALL|wxALIGN_CENTER_VERTICAL, 3);
    sizer_location_copy->Add(label_location_y_copy, 0, wxALL|wxALIGN_CENTER_VERTICAL, 3);
    sizer_location_copy->Add(text_ctrl_location_y, 1, wxALL|wxALIGN_CENTER_VERTICAL, 3);
    sizer_location_copy->Add(label_location_z_copy, 0, wxALL|wxALIGN_CENTER_VERTICAL, 3);
    sizer_location_copy->Add(text_ctrl_location_z, 1, wxALL|wxALIGN_CENTER_VERTICAL|wxADJUST_MINSIZE, 3);
    sizer_location_copy->Add(combo_box_location, 0, wxALL|wxALIGN_CENTER_VERTICAL, 3);
    sizer_2->Add(sizer_location_copy, 0, wxALL|wxEXPAND, 3);
    sizer_7_copy->Add(button_ok, 1, wxALL, 10);
    sizer_7_copy->Add(button_cancel, 1, wxALL, 10);
    sizer_2->Add(sizer_7_copy, 0, wxALL|wxEXPAND, 3);
    SetAutoLayout(true);
    SetSizer(sizer_2);
    sizer_2->Fit(this);
    sizer_2->SetSizeHints(this);
    Layout();
    // end wxGlade
}

void PMDialog::Load(const ClientData_pm * data)
{
  text_ctrl_name->SetValue(data->name);
  text_ctrl_weight->SetValue(wxString::Format("%g",data->weight));
  combo_box_weight->SetStringSelection(data->weight_unit);
  text_ctrl_location_x->SetValue(wxString::Format("%g", data->x));
  text_ctrl_location_y->SetValue(wxString::Format("%g", data->y));
  text_ctrl_location_z->SetValue(wxString::Format("%g", data->z));
  combo_box_location->SetStringSelection(data->loc_unit);
}

void PMDialog::Save(ClientData_pm * data)
{
  double tmp;
  data->name = text_ctrl_name->GetValue();
  if (text_ctrl_weight->GetValue().ToDouble(&tmp))
    data->weight = tmp;
  data->weight_unit = combo_box_weight->GetValue();
  if (text_ctrl_location_x->GetValue().ToDouble(&tmp))
    data->x = tmp;
  if (text_ctrl_location_y->GetValue().ToDouble(&tmp))
    data->y = tmp;
  if (text_ctrl_location_z->GetValue().ToDouble(&tmp))
    data->z = tmp;
  data->loc_unit = combo_box_location->GetValue();  
}

